package com.doyens.gmall.item.controller;


import lombok.SneakyThrows;
import org.junit.Test;

import javax.xml.transform.Source;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CFTest {

    @Test
    public void test1() throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture = new CompletableFuture<>();
        String result = completableFuture.get();
        System.out.println(result);
    }


    @Test
    public void test2() throws ExecutionException, InterruptedException {
        //开启了一个子线程，run方法中是子线程的任务
        //runAsync 没有返回值的子线程任务
        CompletableFuture.runAsync(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        Thread.sleep(500);
                        System.out.println("第一个异步任务");
                        int i = 1/0;
                    }
                }).whenComplete((unused, throwable) -> {
                    System.out.println("第一个任务完成");
                })
                .exceptionally(throwable -> {
                    System.out.println("第一个任务出问题了");
                    return null;
                });
        System.out.println("主线程任务");
        Thread.sleep(1000);


    }

    @Test
    public void test3() throws InterruptedException, ExecutionException {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第二个子任务，有返回值");
            return 100;
        }).whenComplete((integer, throwable) -> {
            System.out.println("二 子任务 执行结果:"+integer);
            //开启子线程？

        });
        System.out.println("主线程任务");
        //get 获取子任务返回值，阻塞线程
//        Integer integer = completableFuture.get();
//        System.out.println("任务执行结果:"+integer);
        Thread.sleep(1000);

    }

    @Test
    public void  test4() throws InterruptedException {
        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务①执行了");
            return "① result";
        });

        //thenRun thenAccept thenApply
        //thenApply 第二个子线程也需要返回值，使用
        //第二个子线程不需要返回值，使用thenRun thenAccept
        completableFuture1.thenApplyAsync(s -> {
            return "";
        });
            completableFuture1.thenRunAsync(() -> {

            });
        completableFuture1.thenAcceptAsync(s -> {
            System.out.println("①的执行结果:"+s);
            System.out.println("任务②执行了");
        });

        completableFuture1.thenAcceptAsync(s -> {
            System.out.println("①的执行结果:"+s);
            System.out.println("任务③执行了");
        });

        completableFuture1.thenAcceptAsync(s -> {
            System.out.println("①的执行结果:"+s);
            System.out.println("任务④执行了");
        });

        System.out.println("主线程");
        Thread.sleep(1000);
    }
}
